﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;
using System.IO;

namespace quanlynhasach
{
    public partial class frmInfosql : Form
    {
        public frmInfosql()
        {
            InitializeComponent();
        }

        public void SaveConnectionString(String newConnString)
        {
            int i = 0;
            SqlConnection connect = new SqlConnection(newConnString);
            try
            {
                connect.Open();
                connect.Close();
                i = 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ket Noi Khong Thanh Cong \n" + ex.Message, "Lỗi Kết Nối", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (i == 1)
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.ConnectionStrings.ConnectionStrings["quanlynhasach.Properties.Settings.QuanLyNhaSachConnectionString"].ConnectionString = newConnString;
                    config.Save();
                    this.Close();
                    Application.Restart();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connect;
            if (rdBtnSuaCSDL.Checked)
            {
                if (checkBoxLogin.Checked)
                    connect = "Data Source=" + Server.Text + ";Initial Catalog=" + data.Text + ";Integrated Security=True";
                else
                    connect = "Data Source=" + Server.Text + ";Initial Catalog=" + data.Text + ";uid=" + use.Text + ";pwd=" + pass.Text;
            }
            else
            {
                try
                {
                    if (checkBoxLogin.Checked)
                        connect = "Data Source=" + Server.Text + ";Integrated Security=True";
                    else
                        connect = "Data Source=" + Server.Text + ";uid=" + use.Text + ";pwd=" + pass.Text;
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    process.StartInfo.FileName = "sqlcmd.exe";
                    process.StartInfo.Arguments = "-S " + Server.Text + " -E  -i csdlchinhsua.sql";
                    process.Start();
                    process.WaitForExit();
                    connect += ";Initial Catalog=QuanLyNhaSach";
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
            SaveConnectionString(connect);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxLogin.Checked)
            {
                use.Enabled = false;
                pass.Enabled = false;
            }
            else
            {
                use.Enabled = true;
                pass.Enabled = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnSuaCSDL.Checked)
                data.Enabled = true;
            else
                data.Enabled = false;
        }

        private void frmInfosql_Load(object sender, EventArgs e)
        {

        }
    }
}
